package com.prime.tutorial.moviestore.service.auth;

import com.prime.tutorial.moviestore.dao.UserActivityDAO;
import com.prime.tutorial.moviestore.dao.UserDAO;
import com.prime.tutorial.moviestore.domain.Users;
import com.prime.tutorial.moviestore.domain.UserActivity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Implementation of {@link AuthService}  <br/>
 * Created by IntelliJ IDEA.<br/>
 * Date: 21 лют 2010 <br/>
 * Time: 0:53:55 <br/>
 * @author iryna.masyuk
 */

@Service
public class AuthServiceImpl implements AuthService {
    UserDAO userDAO;
    UserActivityDAO userActivityDAO;

    @Autowired
    public AuthServiceImpl(UserDAO userDAO,
                           UserActivityDAO userActivityDAO) {
        this.userDAO = userDAO;
        this.userActivityDAO = userActivityDAO;
    }

    public Users login(String login, String password){
        if (!userExists(login)) return null;
        if (!((userDAO.findByLogin(login)).get(0).getPassword().equals(password))) return null;
        return (userDAO.findByLogin(login)).get(0);
    };

    /**
     * Checks whether user exists in database
     * @param login
     * @return
     */
    private boolean userExists(String login){
        if (userDAO.findByLogin(login).isEmpty()) return false;
        return true;
    };

    public void createUserActivity(UserActivity ua){
       userActivityDAO.persist(ua);
    };

    public void updateUserActivity(UserActivity ua){
       userActivityDAO.update(ua);
    };
}
